package view.Core;




import method.camera.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.FrameLayout;
import android.app.ProgressDialog;

public class encodeActivitySub extends Activity {
	//subMenu
    public static final int SearchTreeBtnID = Menu.FIRST;
    public static final int AboutID = Menu.FIRST + 1;
    public static final int helpBtnID = Menu.FIRST + 2;
    
    //button control
    private Button captureButton;
    private Button deCodeButton;
    private Button QRcodeFindButton;
    private EditText QRcodeEditText;
    
    public ProgressDialog processDialog = null;
    
	private QRDecodeCamera view;
	private String qrstring;
	String strQR2, strQR3;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.encodepage3);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		captureButton = (Button)findViewById(R.id.CatureButton);
		deCodeButton = (Button)findViewById(R.id.QRButton);
		QRcodeFindButton = (Button)findViewById(R.id.QRcodeFindButton);
		QRcodeEditText = (EditText)findViewById(R.id.QRcodeEditText);
		
		view = new QRDecodeCamera(this);
		((FrameLayout) findViewById(R.id.preview)).addView(view);
		
		Toast.makeText(encodeActivitySub.this,"Capture Qrcode!!",Toast.LENGTH_LONG).show();
		
		
		captureButton.setOnClickListener(new View.OnClickListener(){
	        @Override
	        public void onClick(View v){
	        	Toast.makeText(encodeActivitySub.this,"Qrcode Capture. You can press the decode button.",Toast.LENGTH_LONG).show();
	        	view.autoFocus();

	        }
		}
		
		
		);
		
		deCodeButton.setOnClickListener(new View.OnClickListener(){
	        @Override
	        public void onClick(View v){
				strQR2 = view.getDecodedX(); 
		        if(strQR2!=""){
		            Toast.makeText(encodeActivitySub.this,"The Qrcode is "+strQR2,Toast.LENGTH_SHORT).show();
	         	/*
		            Intent intent = new Intent();
		            intent.setClass(encodeActivitySub.this, TabLayout_Activities.class);
		            Bundle bundle = new Bundle();
		            bundle.putString("QRid",strQR2);
		            intent.putExtras(bundle);
		            startActivity(intent);
		            
		            */
		            //finish();
		            
		        }
		        else{
		        	Toast.makeText(encodeActivitySub.this,"This is not Qrcode",Toast.LENGTH_LONG).show();
		        }
				}
	        
		}
		
		);
		
		QRcodeFindButton.setOnClickListener(new View.OnClickListener(){
	        @Override
	        public void onClick(View v){
	        	//String inputText = QRcodeEditText.getText().toString();
	        	
	            //Intent intent = new Intent();
	            //intent.setClass(encodeActivitySub.this, TabLayout_Activities.class);
	            //Bundle bundle = new Bundle();
	            //bundle.putString("QRid",inputText);
	            //intent.putExtras(bundle);
	            //startActivity(intent);
	            finish();
	        }
		}
		
		
		);
    }
    

    public boolean onCreateOptionsMenu(Menu menu){
        menu.add(0, SearchTreeBtnID, 0, "About");
        menu.add(0, AboutID, 0, "Exit").setAlphabeticShortcut('e');
        menu.add(0, helpBtnID, 0, "Help");
        
		return super.onCreateOptionsMenu(menu);
    }
    
	// Prepare a option menu
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}
	// Process when select this option menu to do AUTOFOCUS
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	super.onOptionsItemSelected(item);
        switch( item.getItemId() ) 
        {
	       case SearchTreeBtnID:
	        	openDialog();        		
	            break;
	       case AboutID:
	       	 	finish();
	       	 	break;
	       case helpBtnID:
	      	 	finish();
	      	 	break;
        
        }
    	return true;
    	
	}
	//	Open the Dialog
    public void openDialog() 
    {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("This is QR code Decoder use for enter the Qr code to search the tree");
          builder.setTitle("About");
          builder.setPositiveButton("OK",  new DialogInterface.OnClickListener()   {      		 
               @Override
                public void onClick(DialogInterface dialog, int which)    {
                }						
            }).show();
    }
	

}